package com.zpark.controller;

import com.github.pagehelper.PageInfo;
import com.zpark.pojo.User;
import com.zpark.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//@RestController
@Controller
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

//    @GetMapping("/hello/{id}")
//    public User queryById(@PathVariable Integer id){
//        return userService.findById(id);
//    }
//
    @RequestMapping("/delete/{id}")
    public String remove(@PathVariable Integer id, Model model){
        userService.removeById(id);
        return "forward:/user/sele";
    }
//
//    @RequestMapping("/up/{id}")
//   public String chang(User user){
//        userService.changById(user);
//        return "修改用户成功";
//    }
//
//    @RequestMapping("/add/{id}")
//    public String add(User user){
//        userService.addById(user);
//        return "添加用户成功";
//    }
//
    @RequestMapping("/sele")
    public String findAll(Model model){
        List<User> user = userService.findAll();
        model.addAttribute("user", user);
        return "/user/showtable";
    }

   @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable Integer id, Model model){
        log.info("跳转到用户修改页-------------");
        User user = userService.findById(id);
//        User user = new User();
        System.out.println(user);
        model.addAttribute("user",user);
        return "user/update";
}

    @RequestMapping("update")
    public String update(User user,Model model){
        log.info("业务修改运行");
        userService.changById(user);
        model.addAttribute("user",userService.findById(user.getUid()));
        return "/user/show";
    }
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

    @RequestMapping("/save")
    public String toSave(){
         return "/user/save";
    }

    @RequestMapping("/insert")
    public String insert(User user, Model model){
         userService.saveUser(user);
         log.info("开始执行了");
         User user1 = userService.findById(user.getUid());
         model.addAttribute("user", user1);
         return "/user/show";
    }

    @Value("${page.size}")
    String pageSize;
    @RequestMapping("/queryByPage/{curPage}")
    public String queryByPage(@PathVariable("curPage") Integer curPage, Model model) {
        if(curPage < 1)
            curPage = 1;
        PageInfo<User> pageInfo = userService.queryByPage(curPage, Integer.valueOf(pageSize));
        model.addAttribute("pageInfo", pageInfo);
        return "/user/showByPage";
    }
}
